<?php
/**
 * @Author:      sqq
 * @DateTime:    2018-07-09 18:32:48
 * @Description: 用于 ahp 系统用户
 */
class AhpUsersController extends BaseController {

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 18:46:11
     * @Description: 验证token 
     */
    public  function checkToken(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');

        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "checkToken", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-07-07 18:47:37
     * @Description: 用于查看 ahp系统用户的信息
     * @link: http://www.activity.com/ahp/ahp3.0/index?token=3e0ee95d61a08c5ead6f831cbfa0c9a8&page_1&page_size=10
     */
    public function index(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            $ssu = AhpUsers::join('ahp_role','ahp_user.role_id','=','ahp_role.id')->with('ahp_role')->select('ahp_user.id','ahp_user.user_name','ahp_user.real_name','ahp_user.password','ahp_user.role_id','ahp_user.status','ahp_role.role_name')->orderBy('ahp_user.id','desc');
            if(Input::get('id')){
                $ssu->where('ahp_user.id',Input::get('id'));
            }
            if(Input::get('user_name')){
                $ssu->where('user_name','LIKE','%'.Input::get('user_name').'%');
            }
            if(Input::get('real_name')){
                $ssu->where('real_name','LIKE','%'.Input::get('real_name').'%');
            }
            if(Input::get('phone')){
                $ssu->where('phone',Input::get('phone'));
            }

            if(Input::get('begin_data') && Input::get('end_data')){
                $ssu->whereBetween('created_at',array(Input::get('begin_data'),Input::get('end_data')));

            }
            if(Input::get('role_id')){
                $ssu->where('role_id',Input::get('role_id'));
            }
            $count = $ssu->count();
            $ssu=$ssu->skip($page_size*($page-1))->take($page_size)->get();

            //组织返回
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"ssp_sys_users"=>$ssu));
        } while (false);
        Log::info("info", array("system" => "ahp" ,"event" => "index", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 18:49:29
     * @Description: 新增用户
     * @link: http://www.activity.com/ahp/ahp3.0/store?token=3e0ee95d61a08c5ead6f831cbfa0c9a8&user_name=shiqing&real_name=%E6%97%B6%E5%BA%86&phone=15910959938&role_id=1&password=123456
     */
    public function store(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_name' => 'required|max:25',
                'real_name' => 'required',
                //'phone' => 'required|unique:ahp_user,phone',
                'role_id'=>'required',
                'password' => 'required',
                'status' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $messages = $validator->messages();
//                if($messages->has('phone')){
//                    $response = Lang::get("ahp_error.ahp_user_phone_exit_error_2108");
//                    //$response["result"]=$validator->messages()->all();
//                    break;
//                }
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
            $ssu=new AhpUsers();
            $ssu->user_name = Input::get('user_name');
            $ssu->real_name = Input::get('real_name');
            //$ssu->phone = Input::get('phone');
            $ssu->password = Common::make(Input::get('password'));
            $ssu->role_id = Input::get('role_id',0);
            $ssu->desc = Input::get('desc','');
            $ssu->status = Input::get('status',1);
            $ssu->created_at = date("Y-m-d H:i:s",time());
            $ssu->updated_at = date("Y-m-d H:i:s",time());
            $ssu->save();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("ssp_sys_user_id"=>$ssu->id));
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "store", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 19:31:29
     * @Description: 用户详情  也可以用作用户编辑页
     * @link：http://www.activity.com/ahp/ahp3.0/show?token=3e0ee95d61a08c5ead6f831cbfa0c9a8&id=1
     */
    public function show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>'required', //用户id
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }

            $id = Input::get('id');
            $sys_users = AhpUsers::with('ahp_role')->where('id',$id)->first();
            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'id' => $id,
                    'user_name' => $sys_users->user_name,
                    'real_name' => $sys_users->real_name,
                    'phone' => $sys_users->phone,
                    'password' => $sys_users->password,
                    'created_at' => date('Y-m-d H:i:s',strtotime($sys_users->created_at)),
                    'role' => $sys_users->ahp_role->role_name,
                    'role_id' => $sys_users->ahp_role->id,
                    'status' => $sys_users->status,
                )
            );

        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 19:35:29
     * @Description: 编辑 保存 用户
     * @link: http://www.activity.com/ahp/ahp3.0/update?token=3e0ee95d61a08c5ead6f831cbfa0c9a8&id=11&user_name=shiqing&real_name=%E6%97%B6%E5%BA%86%E5%BA%86&phone=15910959936&password=123456&role_id=1
     */
    public function update(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>'required',
                'user_name'=>'required',
                'real_name'=>'required',
                //'phone' => 'required|unique:ahp_user,phone',
                'password'=>'required',
                'role_id'=>'required',
                'status'=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $messages = $validator->messages();
//                if($messages->has('phone')){
//                    $response = Lang::get("ahp_error.ahp_user_phone_exit_error_2108");
//                    //$response["result"]=$validator->messages()->all();
//                    break;
//                }
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }

            $sys_user=AhpUsers::find(Input::get('id'));
            if(is_null($sys_user)){
                $response = Lang::get("ahp_error.user_not_exit_2101");
                break;
            }
            $sys_user->user_name=Input::get("user_name");
            $sys_user->real_name=Input::get("real_name");
            $sys_user->phone=Input::get("phone");
            $sys_user->status=Input::get('status');
            if($sys_user->password!=Input::get('password')){
                $sys_user->password=Common::make(Input::get('password'));
            }
            $sys_user->role_id = Input::get('role_id');
            $sys_user->updated_at = date("Y-m-d H:i:s",time());
            $sys_user->save();
            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "update", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09
     * @Description: 平台端验证验证码
     */
    public function verify(){
        $validator = Validator::make(Input::all(),
            array(
                'phone' => 'required',
                'check_code' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                break;
            }
            $check_code = UserCheckCode::getCheckCode(Input::get('phone'),'086');
            if(is_null($check_code)){
                $response = Lang::get('error.user_login_checkcode_null_1012');
                break;
            }
            if(time() > Common::datetime2Num($check_code->out_time_at))
            {
                $response = Lang::get('error.user_login_checkcode_timeout_1013');
                break;
            }
            if($check_code->check_code != Input::get('check_code'))
            {
                $response = Lang::get('error.user_login_checkcode_error_1014');
                break;
            }
            $result = AhpUsers::where('sys_phone', Input::get('phone'))->first();
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $result,
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "verify", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09
     * @Description: 修改密码
     */
    public function editPwd(){
        $validator = Validator::make(Input::all(),
            array(
                'sys_account' => 'required',
                'password' => 'required',
                'password_confirmation' => 'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get('ahp_error.args_error_1000');
                break;
            }
            if(Input::get('password') == 'undefined') {
                $response = Lang::get('ahp_error.args_error_1000');
                break;
            }
            if(Input::get('password') != Input::get('password_confirmation'))
            {
                $response = Lang::get('ahp_error.user_password_differ_error_3104');
                break;
            }
            $obj = SspSysUsers::where('sys_account', Input::get('sys_account'))->first();
            if(!is_null($obj)) {
                try{
                    SspSysUsers::where('sys_account', Input::get('sys_account'))->update(['sys_password' => Common::make(Input::get('password'))]);
                }catch (Exception $e){
                    Log::error("error", array("system" => "ahp" ,"event" => "editPwd", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("ahp_error.user_edit_pwd_error_3105");
                    break;
                }
                $response = array(
                    'code' => 0,
                    'msg' => 'ok',
                    'result' => array()
                );
            }else{
                $response = Lang::get('ahp_error.user_not_exit_2101');
            }
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "editPwd", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 18:55:26
     * @Description: 平台端端修改密码
     */
    public function updatePwd(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'password' => 'required',
                'new_password' => 'required',
                'password_confirmation' => 'required',
            )
        );
        $response = array('code' => 0,'msg' => 'ok',);
        do{
            if($validator->fails()) {
                $response = Lang::get('ahp_error.args_error_1000');
                break;
            }
            if(Input::get('new_password') != Input::get('password_confirmation'))
            {
                $response = Lang::get('ahp_error.user_password_differ_error_3104');
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
            if($ssp_sys_user->password != Common::make(Input::get('password'))){
                $response = Lang::get('ahp_error.user_old_password_error_3106');
                break;
            }
            try{
                $ssp_user = AhpUsers::find($ssp_sys_user->id);
                $ssp_user->password = Common::make(Input::get('new_password'));
                $ssp_user->save();
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "updatePwd", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.user_edit_pwd_error_3105");
                break;
            }
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "updatePwd", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 18:50:26
     * @Description:  管理员 状态修改
     * @link: http://www.activity.com/ahp/ahp3.0/status?token=3e0ee95d61a08c5ead6f831cbfa0c9a8&id=11&status=2
     */
    public function status(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'status' => 'required',
            )
        );
        $response = array('code' => 0,'msg' => 'ok',);
        do{
            if($validator->fails()) {
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"]=$validator->messages()->all();
                break;
            }

            $sys_user=AhpUsers::find(Input::get('id'));
            if(is_null($sys_user)){
                $response = Lang::get("ahp_error.user_not_exit_2101");
                break;
            }
            $sys_user->status = Input::get('status');
            $sys_user->updated_at = date('Y-m-d H:i:s',time());
            $sys_user->save();
            $response = array('code' => 0, 'msg' => 'ok');

        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "status", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }


}